//
//  RCRecommendFoodDetailHeaderCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCRecommendFoodDetailHeaderCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"
#import "UIImageView+WebCache.h"

#define kBGImageFile             @"image_member_cell_bg.png"
#define kDefaultImageFile        @"image_default.png"
#define kLineImageFile           @"image_line_type3.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"

@implementation RCRecommendFoodDetailHeaderCell{
    UILabel *_merchantNameLabel;
    UILabel *_descLabel;
    UILabel *_priceLabel;
    UIImageView *_placeImageView;
    UIButton *_transParentButton;
    UILabel *_distanceLabel;
    UIImageView *_lineImageView;
    UIImageView *_merchantImageView;
    UIImageView *_startImageView1;
    UIImageView *_startImageView2;
    UIImageView *_startImageView3;
    UIImageView *_startImageView4;
    UIImageView *_startImageView5;
    UILabel     *_scoreLabel;
}

@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        self.layer.contents = (id)bgImage.CGImage;
        [self.contentView setBackgroundColorWithImage:bgImage];
        [self setUpView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
    
}

-(void)dealloc{
    self.delegate = nil;
    [super dealloc];
}


- (void) setUpView
{
    _merchantNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 13, 300, 20)];
    _merchantNameLabel.font = [UIFont fontWithName:kFontNameHeiti size:19];
    _merchantNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    _merchantNameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_merchantNameLabel];
    [_merchantNameLabel release];
    
    UIImageView *defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 44, 57, 57)];
    [defaultImageView setImage:[UIImage imageNamed:kDefaultImageFile]];
    [self addSubview:defaultImageView];
    [defaultImageView release];
    
    _merchantImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 45, 55, 55)];
    _merchantImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_merchantImageView];
    [_merchantImageView release];
    
    _transParentButton = [[UIButton alloc]initWithFrame:defaultImageView.frame];
    _transParentButton.backgroundColor = [UIColor clearColor];
    [_transParentButton addTarget:self action:@selector(transparentButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _transParentButton.hidden = YES;
    [self addSubview:_transParentButton];
    [_transParentButton release];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(85, 50, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView1];
    [_startImageView1 release];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 50, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView2];
    [_startImageView2 release];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(115, 50, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView3];
    [_startImageView3 release];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(130, 50, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView4];
    [_startImageView4 release];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(145, 50, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView5];
    [_startImageView5 release];
    
    _scoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(162, 50, 20, 14)];
    _scoreLabel.backgroundColor = [UIColor clearColor];
    _scoreLabel.textColor = [UIColor redColor];
    _scoreLabel.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    [self addSubview:_scoreLabel];
    [_scoreLabel release];
    
    UIImage *img = [UIImage imageNamed:@"image_allCity_district"];
    _placeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(253, 50, img.size.width, img.size.height)];
    _placeImageView.image = img;
    [self addSubview:_placeImageView];
    [_placeImageView release];
    
    
    _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(253+img.size.width+5, 50, 70, 14)];
    _distanceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _distanceLabel.textColor = [UIColor colorWithHex:0xff989797];
    _distanceLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_distanceLabel];
    [_distanceLabel release];
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 70, 200, 12)];
    _priceLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _priceLabel.textColor = [UIColor colorWithHex:0xff989797];
    _priceLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_priceLabel];
    [_priceLabel release];
    
    
    _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 88, 200, 12)];
    _descLabel.font = [UIFont fontWithName:kFontNameHeiti size:12];
    _descLabel.textColor = [UIColor colorWithHex:0xff989797];
    _descLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_descLabel];
    [_descLabel release];
    
    
    
    _lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 109, 320, 1)];
    _lineImageView.backgroundColor = [UIColor clearColor];
    NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kLineImageFile];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    [self.contentView addSubview:_lineImageView];
    [_lineImageView release];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
}
- (void) bindData:(RCRecommendFoodDetailDataModel *)detailModel
{
    if (nil == detailModel) {
        return;
    }
    NSString *titleName = nil;
    if (![NSString stringIsNullOrEmpty:detailModel.shopName]) {
        titleName = [NSString stringWithFormat:@"【%@】%@",detailModel.shopName,detailModel.foodName];
    }else{
        titleName = detailModel.foodName;
    }
    _merchantNameLabel.text = titleName;
    
    NSInteger score = detailModel.foodScore;
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
    UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
    [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
    [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
    [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
    [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
    [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    
    _scoreLabel.text = [NSString stringWithFormat:@"%d",score];
    
    
    NSString *distance = nil;
    if ([detailModel.foodDistance floatValue] > 1000) {
        float distanceKm = [detailModel.foodDistance floatValue]/1000.0;
        if ( ((int)distanceKm) > 999 )
            distance = [NSString stringWithFormat:@"%d+km",999];
        else
            distance = [NSString stringWithFormat:@"%.1fkm",distanceKm];
    }else{
        distance = [NSString stringWithFormat:@"%@m",detailModel.foodDistance];
    }
    _distanceLabel.text = distance;
    
    _transParentButton.hidden = YES;
    if (![NSString stringIsNullOrEmpty:detailModel.foodImageUrl]) {
        _transParentButton.hidden = NO;
        [_merchantImageView setImageWithURL:[NSURL URLWithString:detailModel.foodImageUrl] placeholderImage:[UIImage imageNamed:kDefaultImageFile]];
    }
    
    if (![NSString stringIsNullOrEmpty:detailModel.foodPrice]) {
        _priceLabel.text = detailModel.foodPrice;
    }else{
        _priceLabel.text = @"";
    }
    
    
    if (![NSString stringIsNullOrEmpty:detailModel.foodDesc]) {
        _descLabel.text = detailModel.foodDesc;
    }else{
        _descLabel.text = @"";
    }
    
}

-(void)transparentButtonClicked{
    if (_delegate && [_delegate respondsToSelector:@selector(gotoFoodPictureAlbumView)]) {
        [_delegate gotoFoodPictureAlbumView];
    }
}

@end
